/**
 * 
 */
package com.shine.icis.blo.imp;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IAccrualInfoService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.dao.IAccrualInfoDAO;
import com.shine.icis.hbean.AccrualInfo;
import com.shine.icis.exception.ICISException;

/**
 * @author zsy
 *
 */
public class AccrualInfoService implements IAccrualInfoService {
	
	private IAccrualInfoDAO accrualInfoDAO;

	
	public void setAccrualInfoDAO(IAccrualInfoDAO accrualInfoDAO) {
		this.accrualInfoDAO = accrualInfoDAO;
	}

	@Override
	public void addAccrualInfo(int tradeDate, String arapType, String oppPar, String mktCodeSipf, 
			String arapBusiType, String curTypeSipf, String arapReason, double arapAmt, 
			int harapDate, Integer arapDate, String summary, int busiDate) throws ICISException {
		if (arapType == null || oppPar == null || mktCodeSipf == null 
				|| arapBusiType == null || curTypeSipf == null) {
			throw new IllegalArgumentException();
		}
		boolean check = "0".equals(SysCodeDictLoader.getInstance().getSubmitConfValue("ACCRUAL_MAN_TYPE"));
		if (check && !accrualInfoDAO.checkId(tradeDate, arapType, oppPar, 
				mktCodeSipf, arapBusiType, curTypeSipf, harapDate, busiDate)) {
			throw new ICISException("应收应付信息表的主键不能重复");
		}
		AccrualInfo info = new AccrualInfo();
		info.setTradeDate(tradeDate);
		info.setArapType(arapType);
		info.setOppPar(oppPar);
		info.setMktCodeSipf(mktCodeSipf);
		info.setArapBusiType(arapBusiType);
		info.setCurTypeSipf(curTypeSipf);
		info.setHarapDate(harapDate);
		info.setBusiDate(busiDate);
		info.setArapReason(arapReason);
		info.setArapAmt(arapAmt);
		info.setArapDate(arapDate);
		info.setSummary(summary);
		accrualInfoDAO.save(info);
	}
	
	@Override
	public void modifyAccrualInfo(long id, int tradeDate, String arapType, String oppPar, String mktCodeSipf, 
			String arapBusiType, String curTypeSipf, String arapReason, double arapAmt, 
			int harapDate, Integer arapDate, String summary, int busiDate) throws ICISException {
		if (arapType == null || oppPar == null || mktCodeSipf == null 
				|| arapBusiType == null || curTypeSipf == null) {
			throw new IllegalArgumentException();
		}
		AccrualInfo info = accrualInfoDAO.findById(id);
		if (info == null) {
			throw new IllegalArgumentException();
		}
		boolean check = "0".equals(SysCodeDictLoader.getInstance().getSubmitConfValue("ACCRUAL_MAN_TYPE"));
		if (check){
			boolean equals = false;//复合主键值是否有变动，不变动不需要重复检查
			if (info.getTradeDate() == tradeDate 
					&& arapType != null && arapType.equals(info.getArapType()) 
					&& oppPar != null && oppPar.equals(info.getOppPar()) 
					&& mktCodeSipf != null && mktCodeSipf.equals(info.getMktCodeSipf()) 
					&& arapBusiType != null && arapBusiType.equals(info.getArapBusiType()) 
					&& curTypeSipf != null && curTypeSipf.equals(info.getCurTypeSipf())
					&& harapDate == info.getHarapDate()
					&& busiDate == info.getBusiDate()) {
				equals = true;
			}
			
			if (!equals && !accrualInfoDAO.checkId(tradeDate, arapType, oppPar, 
					mktCodeSipf, arapBusiType, curTypeSipf, harapDate, busiDate)) {
				throw new ICISException("应收应付信息表的主键不能重复");
			}
		}
		info.setTradeDate(tradeDate);
		info.setArapType(arapType);
		info.setOppPar(oppPar);
		info.setMktCodeSipf(mktCodeSipf);
		info.setArapBusiType(arapBusiType);
		info.setCurTypeSipf(curTypeSipf);
		info.setHarapDate(harapDate);
		info.setArapReason(arapReason);
		info.setArapAmt(arapAmt);
		info.setArapDate(arapDate);
		info.setSummary(summary);
		
		accrualInfoDAO.update(info);
		
	}

	@Override
	public void deleteAccrualInfo(long id) {
		AccrualInfo ai = accrualInfoDAO.findById(id);
		if (ai == null) {
			return;
		}
		accrualInfoDAO.delete(ai);
		
	}

	@Override
	public ListPage<AccrualInfo> getAccrualInfoPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		if (qp.getParameter("arapType") == null) {//查找汇总的
			return accrualInfoDAO.findStatListPage(qp);
		}
		return accrualInfoDAO.findListPage(qp);
	}

}
